package com.springboot.demo.controller;

import com.cx.item.common.par.check.CheckParTarget;
import com.cx.item.common.par.check.CheckParTargetInstance;
import com.cx.item.common.vo.ResultPageVo;
import com.springboot.demo.model.model.CustomerAddress;
import com.springboot.demo.model.model.CustomerLoginInfo;
import com.springboot.demo.model.par.AddressAddOrUpdatePar;
import com.springboot.demo.model.par.UpdateCustomerPar;
import com.springboot.demo.model.vo.AddressDetailsVo;
import com.springboot.demo.model.vo.AddressListVo;
import com.springboot.demo.model.vo.CustomerDetailsVo;
import com.springboot.demo.service.CustomerAddressService;
import com.springboot.demo.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 用户信息
 * Created by hwm on 2018/6/20.
 */
@Api(description = "用户api")
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController{

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerAddressService customerAddressService;

    @ApiOperation("更新用户信息")
    @RequestMapping(value = "/updateCustomer", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo updateCustomer(UpdateCustomerPar updateCustomerPar) {

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        updateCustomerPar.setCustomerId(currUserLoginInfo.getCustomerId());

        int i = customerService.updateCustomer(updateCustomerPar);

        return ResultPageVo.success(i);
    }

    @ApiOperation("获取用户信息")
    @RequestMapping(value = "/getCustomerDetails", method = RequestMethod.GET)
    @ResponseBody
    public ResultPageVo<CustomerDetailsVo> getCustomerDetails() {

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        CustomerDetailsVo customerDetailsVo = customerService.getCustomerDetails(currUserLoginInfo.getCustomerId());

        return ResultPageVo.success(customerDetailsVo);
    }

    @ApiOperation("删除地址")
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo deleteAddress(Integer addressId) {

        if (addressId == null) {
            return ResultPageVo.failure("addressId为空", null);
        }

        CustomerAddress addressUp = new CustomerAddress();
        addressUp.setAddressId(addressId);
        addressUp.setAddressStatus(0);
        addressUp.setUpdateTime(new Date());
        addressUp.setRemark("删除地址");
        int i = customerAddressService.updateByPrimaryKeySelective(addressUp);

        return ResultPageVo.success(i);
    }

    @ApiOperation("获取地址详情")
    @RequestMapping(value = "/getAddressDetails", method = RequestMethod.GET)
    @ResponseBody
    public ResultPageVo getAddressDetails(Integer addressId) {

        if (addressId == null) {
            return ResultPageVo.failure("addressId为空", null);
        }

        AddressDetailsVo addressDetailsVo = customerService.getAddressDetails(addressId);

        return ResultPageVo.success(addressDetailsVo);
    }

    @ApiOperation("更新地址")
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo updateAddress(AddressAddOrUpdatePar addressAddOrUpdatePar) {

        CheckParTarget checkParTarget = CheckParTargetInstance.getInstance(addressAddOrUpdatePar, "addressId");
        checkParTarget.checkParIsNullThrowError();

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        addressAddOrUpdatePar.setCustomerId(currUserLoginInfo.getCustomerId());

        int i = customerService.updateAddress(addressAddOrUpdatePar);

        return ResultPageVo.success(i);
    }

    @ApiOperation("添加地址")
    @RequestMapping(value = "/addAddress", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo addAddress(AddressAddOrUpdatePar addressAddOrUpdatePar) {

        CheckParTarget checkParTarget = CheckParTargetInstance.getInstance(addressAddOrUpdatePar
                , "provinceName", "cityName", "zoneName", "detailedAddress", "name", "phone", "isDefault");
        checkParTarget.checkParIsNullThrowError();

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        addressAddOrUpdatePar.setCustomerId(currUserLoginInfo.getCustomerId());

        int i = customerService.addAddress(addressAddOrUpdatePar);

        return ResultPageVo.success(i);
    }

    @ApiOperation("地址列表")
    @RequestMapping(value = "/getAddressList", method = RequestMethod.GET)
    @ResponseBody
    public ResultPageVo<List<AddressListVo>> getAddressList() {

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        List<AddressListVo> addressList = customerService.getAddressList(currUserLoginInfo.getCustomerId());

        return ResultPageVo.success(addressList);
    }

}
